package com.xyy.controller;

import com.xyy.common.Result;
import com.xyy.entity.Patient;
import com.xyy.service.PatientService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 就诊人Controller
 */
@RestController
@RequestMapping("/patient")
public class PatientController {
    
    @Resource
    private PatientService patientService;
    
    /**
     * 添加就诊人
     */
    @PostMapping("/add")
    public Result<Patient> addPatient(@Valid @RequestBody Patient createDTO, HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            if (userId == null) {
                return Result.error("用户未登录或登录信息无效");
            }
            Patient patient = patientService.addPatient(createDTO, userId);
            return Result.success(patient);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除就诊人
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deletePatient(@PathVariable Long id, HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            if (userId == null) {
                return Result.error("用户未登录或登录信息无效");
            }
            boolean success = patientService.deletePatient(id, userId);
            if (success) {
                return Result.success(true);
            } else {
                return Result.error("就诊人不存在或无权限删除");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 修改就诊人信息
     */
    @PutMapping("/update")
    public Result<Patient> updatePatient(@Valid @RequestBody Patient patient, HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            if (userId == null) {
                return Result.error("用户未登录或登录信息无效");
            }
            Patient updatedPatient = patientService.updatePatient(patient, userId);
            return Result.success(updatedPatient);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取就诊人列表
     */
    @GetMapping("/list")
    public Result<List<Patient>> getPatientList(HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            if (userId == null) {
                return Result.error("用户未登录或登录信息无效");
            }
            List<Patient> patients = patientService.getPatientList(userId);
            return Result.success(patients);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据ID获取就诊人
     */
    @GetMapping("/{id}")
    public Result<Patient> getPatientById(@PathVariable Long id, HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            if (userId == null) {
                return Result.error("用户未登录或登录信息无效");
            }
            Patient patient = patientService.getPatientById(id, userId);
            if (patient != null) {
                return Result.success(patient);
            } else {
                return Result.error("就诊人不存在或无权限查看");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
} 